package com.lc.file;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

/**
 * @author 逝兮诚
 * @date 2017年6月21日 上午10:55:55
 * 通道复制
 */
public class TransferFromTest {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {
			RandomAccessFile fromFile = new RandomAccessFile("formFile.txt", "rw");
			FileChannel fChanner = fromFile.getChannel();
			
			RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
			FileChannel toChanner = toFile.getChannel();
			long position = 0;
			long count = fChanner.size();
			long transferLength = fChanner.transferTo(position, count, toChanner);
			
			toChanner.position(0);
			System.out.println("transferLength:" + transferLength);
			
			ByteBuffer buf = ByteBuffer.allocate(48);
			int readSize = 0;
			do {
				readSize = toChanner.read(buf);
				buf.flip();
				System.out.println(Charset.forName("UTF-8").decode(buf));
				buf.clear();
			} while (readSize != -1);

		} catch ( IOException e) {
			e.printStackTrace();
		} 
	}

}
